﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PlasmaMes.Services;

namespace PlasmaMes
{
    public partial class Form_KYPLC: Form
    {
        public Form_KYPLC()
        {
            InitializeComponent();
        }

        private async void bt_读取_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tb_读取地址.Text))
            {
                MessageBox.Show("读取地址不能为空");
                return;

            }

            string msg = "RD" + " " + $"{tb_读取地址.Text}" + "\r";

            // 显示“正在读取”
            uiRichTextBox_读取.Text = "正在读取...";

            string result = await TcpService.Instance.ReadFormPLC(msg);

            uiRichTextBox_读取.Clear();

            //uiRichTextBox_读取.Text = TcpService.Instance.tcpResponse;

            uiRichTextBox_读取.Text = result;
        }

        private async void bt_写入_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tb_写入地址.Text))
            {
                MessageBox.Show("写入地址不能为空");
                return;
            }


            if (string.IsNullOrEmpty(uiRichTextBox_写入.Text))
            {
                MessageBox.Show("写入内容不能为空");
                return;
            }

            string msg = "WR" + " " + $"{tb_写入地址.Text}" + " " + $"{int.Parse(uiRichTextBox_写入.Text)}" + "\r";

            await TcpService.Instance.Tcp_SemdMsg(msg);
        }
    }
}
